#include "getinftonet.h"
#include<QTextCodec>
#include<QHttpPart>
#include<QHttpMultiPart>
#include<QNetworkRequest>
#include<QString>
#include<QTimer>
Getinftonet::Getinftonet(QObject *parent) : QObject(parent)
{
    netinit(); //网络初始化
         getshopinftosend();
    //     getshopinfuptosend();

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this,SLOT(getmenulist()));
    timer->start(5000);

    idd = 0;
}


void Getinftonet::netinit(){
    network_shopinf = new QNetworkAccessManager();
    network_shopinfreq = new QNetworkRequest();
    connect(network_shopinf, &QNetworkAccessManager::finished, this, &Getinftonet::replyFinished);

    network_shopinfimg = new QNetworkAccessManager();
    network_shopinfimgreq = new QNetworkRequest();
    connect(network_shopinfimg, &QNetworkAccessManager::finished, this, &Getinftonet::getshopinftoimg);


    network_shopinfup = new QNetworkAccessManager();
    network_shopinfupreq = new QNetworkRequest();
    connect(network_shopinfup, &QNetworkAccessManager::finished, this, &Getinftonet::getmenutoup);


    network_sed = new QNetworkAccessManager();
    network_req = new QNetworkRequest();
    connect(network_sed, &QNetworkAccessManager::finished, this, &Getinftonet::getshopinftoup);

    network_menulist = new QNetworkAccessManager();
    network_menulistreq = new QNetworkRequest();
    connect(network_menulist, &QNetworkAccessManager::finished, this, &Getinftonet::slotgetmenulist);
}

void Getinftonet::getshopinftoimg(QNetworkReply *reply){
    //获取响应的信息，状态码为200表示正常
    QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);

    //无错误返回
    if(reply->error() == QNetworkReply::NoError)
    {
        QByteArray bytes = reply->readAll();  //获取字节

        emit shopinfimg(&bytes);
    }
    else
    {
        //处理错误
        qDebug()<<"处理错误";
    }
}


void Getinftonet::getshopinftoup(QNetworkReply *reply){
    //获取响应的信息，状态码为200表示正常
    QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);

    //无错误返回
    if(reply->error() == QNetworkReply::NoError)
    {
        QByteArray bytes = reply->readAll();  //获取字节
        QString result(bytes);  //转化为字符串
        qDebug()<<"result"+result;
        QTextCodec *t = QTextCodec::codecForName("UTF8");
        QByteArray ba = bytes;
        QString jsons =t->toUnicode(ba);
        qDebug()<<"json"+jsons;
    }
    else
    {
        //处理错误
        qDebug()<<"getshopinftoup处理错误";
    }
    reply->deleteLater();
}

void Getinftonet::replyFinished(QNetworkReply *reply){
    //获取响应的信息，状态码为200表示正常
    QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);

    //无错误返回
    if(reply->error() == QNetworkReply::NoError)
    {
        QByteArray bytes = reply->readAll();  //获取字节
        QString result(bytes);  //转化为字符串
        QTextCodec *t = QTextCodec::codecForName("UTF8");
        QByteArray ba = bytes;
        QString jsons =t->toUnicode(ba);
        qDebug()<<"begin";
        jsontoshopinf(jsons);  //开始解析数据
    }
    else
    {
        //处理错误
        qDebug()<<"处理错误";
    }
}


void Getinftonet::jsontoshopinf(QString json)  //开始解析数据
{
    qDebug()<<"搜索解析";
    QJsonParseError json_error;
    QJsonDocument parse_doucment = QJsonDocument::fromJson(json.toUtf8(), &json_error);
    if(json_error.error == QJsonParseError::NoError){
        QJsonObject rootObj = parse_doucment.object();
        if(rootObj.contains("Date")){
            QJsonValue valuedata = rootObj.value("Date");
            QJsonObject valueArray = valuedata.toObject();
            structshopinf shinf;
            if(valueArray.contains("ShopName"))
            {
                QJsonValue album_id = valueArray.take("ShopName");
                if(album_id.isString())  shinf.ShopName = album_id.toString();
            }
            if(valueArray.contains("ip"))
            {
                QJsonValue album_id = valueArray.take("ip");
                if(album_id.isString())  shinf.ip = album_id.toString();
            }
            if(valueArray.contains("IsLogin"))
            {
                QJsonValue album_id = valueArray.take("IsLogin");
                if(album_id.isString())  shinf.IsLogin = album_id.toString();
            }
            if(valueArray.contains("photopath"))
            {
                QJsonValue album_id = valueArray.take("photopath");
                if(album_id.isString()){
                    //shinf.photopath = album_id.toString();
                    qDebug()<<album_id.toString();
                    getshopinfimgtosend(album_id.toString());
                }
            }
            if(valueArray.contains("text"))
            {
                QJsonValue album_id = valueArray.take("text");
                if(album_id.isString())  shinf.text = album_id.toString();
            }
            if(valueArray.contains("addr"))
            {
                QJsonValue album_id = valueArray.take("addr");
                if(album_id.isString())  shinf.addr = album_id.toString();
            }
            if(valueArray.contains("PhoneNumber"))
            {
                QJsonValue album_id = valueArray.take("PhoneNumber");
                if(album_id.isString())  shinf.PhoneNumber = album_id.toString();
            }
            if(valueArray.contains("shoper"))
            {
                QJsonValue album_id = valueArray.take("shoper");
                if(album_id.isString())  shinf.shoper = album_id.toString();
            }
            if(valueArray.contains("shoptyp"))
            {
                QJsonValue album_id = valueArray.take("shoptyp");
                if(album_id.isString())  shinf.shoptyp = album_id.toString();
            }
            if(valueArray.contains("fdstype"))
            {
                QJsonValue album_id = valueArray.take("fdstype");
                if(album_id.isString())  shinf.fdstype = album_id.toString();
            }
            emit getshopinftojson(&shinf);
        }
    }

}



void Getinftonet::getmenutoup(QNetworkReply *reply){
    //获取响应的信息，状态码为200表示正常
    QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);

    //无错误返回
    if(reply->error() == QNetworkReply::NoError)
    {
        QByteArray bytes = reply->readAll();  //获取字节
        QString result(bytes);  //转化为字符串
        QTextCodec *t = QTextCodec::codecForName("UTF8");
        QByteArray ba = bytes;
        QString jsons =t->toUnicode(ba);
        qDebug()<<"begin";
        getparamtomenu(jsons);  //开始解析数据
    }
    else
    {
        //处理错误
        qDebug()<<"处理错误";
    }
}


void Getinftonet::getparamtomenu(QString json)  //开始解析数据
{
    qDebug()<<"搜索解析";
    QJsonParseError json_error;
    QJsonDocument parse_doucment = QJsonDocument::fromJson(json.toUtf8(), &json_error);
    if(json_error.error == QJsonParseError::NoError){
        QJsonObject rootObj = parse_doucment.object();
        if(rootObj.contains("Date")){
            QJsonValue valuedata = rootObj.value("Date");
            QJsonArray heeloArray = valuedata.toArray();
            QVector<mymenu> menulist;
            mymenu menu;

            for (int i = 0; i < heeloArray.size(); i++){
                QJsonValue helloArrayValue = heeloArray.at(i);
                if(helloArrayValue.isObject()){
                    QJsonObject jsobj = helloArrayValue.toObject();

                    if(jsobj.contains("id"))
                    {
                        QJsonValue album_id = jsobj.take("id");
                        menu.id = album_id.toString();
                    }
                    if(jsobj.contains("name"))
                    {
                        QJsonValue album_name = jsobj.take("name");
                        menu.name = album_name.toString();
                    }
                    if(jsobj.contains("hot"))
                    {
                        QJsonValue album_hot = jsobj.take("hot");
                        menu.hot = album_hot.toString();
                    }
                    if(jsobj.contains("minet"))
                    {
                        QJsonValue album_minet = jsobj.take("minet");
                        menu.minet = album_minet.toString();
                    }
                    if(jsobj.contains("money"))
                    {
                        QJsonValue album_money = jsobj.take("money");
                        menu.money = album_money.toString();
                    }
                    if(jsobj.contains("photo"))
                    {
                        QJsonValue album_photo = jsobj.take("photo");
                        menu.photo.append(album_photo.toString());
                    }
                    if(jsobj.contains("eattype"))
                    {
                        QJsonValue album_eattype = jsobj.take("eattype");
                        menu.eattype = album_eattype.toString();
                    }

                }
                menulist.append(menu);
                menu.photo.clear();
            }
            emit sigmenus(menulist);
        }
    }
}

void Getinftonet::slotgetmenulist(QNetworkReply *reply){
    //获取响应的信息，状态码为200表示正常
    QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);

    //无错误返回
    if(reply->error() == QNetworkReply::NoError)
    {
        QByteArray bytes = reply->readAll();  //获取字节
        QString result(bytes);  //转化为字符串
        QTextCodec *t = QTextCodec::codecForName("UTF8");
        QByteArray ba = bytes;
        QString jsons =t->toUnicode(ba);
        qDebug()<<"begin";
        jsonmenulist(jsons);  //开始解析数据
    }
    else
    {
        //处理错误
        qDebug()<<"处理错误";
    }
}



void Getinftonet::jsonmenulist(QString json)  //开始解析数据
{
    qDebug()<<"订单解析";
    QJsonParseError json_error;
    QJsonDocument parse_doucment = QJsonDocument::fromJson(json.toUtf8(), &json_error);
    if(json_error.error == QJsonParseError::NoError){
        QJsonObject rootObj = parse_doucment.object();
        if(rootObj.contains("Date")){
            if(rootObj.take("Date").toInt() != 1) return;
        }
        clihnum shinf;
        if(rootObj.contains("dt")){
            QJsonValue valuedata = rootObj.value("dt");
            QJsonObject valueinf = valuedata.toObject();
            if(valueinf.contains("id")) shinf.id = valueinf.take("id").toString();
            idd = shinf.id.toInt();
            if(valueinf.contains("uid")) shinf.uid = valueinf.take("uid").toString();
            if(valueinf.contains("value")) shinf.value = valueinf.take("value").toString();
            if(valueinf.contains("ischick")) shinf.ischick = valueinf.take("ischick").toString();
            if(valueinf.contains("mony")) shinf.mony = valueinf.take("mony").toString();
            if(valueinf.contains("time")) shinf.time = valueinf.take("time").toString();

        //*********************//

        if(valueinf.contains("Menu")){
            QJsonValue valuedata = valueinf.value("Menu");
            QJsonArray heeloArray = valuedata.toArray();
            menut menu;


            for (int i = 0; i < heeloArray.size(); i++){
                QJsonValue helloArrayValue = heeloArray.at(i);
                if(helloArrayValue.isObject()){
                    QJsonObject valueArray = helloArrayValue.toObject();

                    if(valueArray.contains("id"))
                    {
                        QJsonValue album_id = valueArray.take("id");
                        menu.id = album_id.toString();
                    }
                    if(valueArray.contains("name"))
                    {
                        QJsonValue album_name = valueArray.take("name");
                        menu.name = album_name.toString();
                    }
                    if(valueArray.contains("hot"))
                    {
                        QJsonValue album_hot = valueArray.take("hot");
                        menu.hot = album_hot.toString();
                    }
                    if(valueArray.contains("money"))
                    {
                        QJsonValue album_money = valueArray.take("money");
                        menu.money = album_money.toString();
                    }
                    if(valueArray.contains("photo"))
                    {
                        QJsonValue album_photo = valueArray.take("photo");
                        menu.photo = album_photo.toString();
                    }
                    if(valueArray.contains("eattype"))
                    {
                        QJsonValue album_eattype = valueArray.take("eattype");
                        menu.eattype = album_eattype.toString();
                    }
                    if(valueArray.contains("type"))
                    {
                        QJsonValue album_eattype = valueArray.take("type");
                        menu.type = album_eattype.toString();
                    }
shinf.menus.append(menu);
                }

            }

             }
        emit sigmenulist(shinf);


        }
    }
}

/*获取店铺信息*/
void Getinftonet::getshopinftosend(){
    QString KGAPISTR1 = QString("http://hunya.fun:9000/clishowshop?unit=%1").arg(shopunit);
    qDebug()<<KGAPISTR1;

    network_shopinfreq->setUrl(QUrl(KGAPISTR1));
    network_shopinf->get(*network_shopinfreq);
}

void Getinftonet::getshopinfimgtosend(QString url){
    network_shopinfimgreq->setUrl(QUrl(url));
    network_shopinfimg->get(*network_shopinfimgreq);

}
void Getinftonet::getshopinfuptosend(){
    network_shopinfupreq->setUrl(QUrl(QString("http://hunya.fun:9000/cligetnum?unit=%1").arg(shopunit)));
    network_shopinfup->get(*network_shopinfupreq);

}

void Getinftonet::getmenulist(){
    network_menulistreq->setUrl(QUrl(QString("http://hunya.fun:9000/clihat?unit=%1&id=%2").arg(shopunit).arg(idd)));
    network_menulist->get(*network_menulistreq);
}


/* 更新店铺信息 */
void Getinftonet::slotshopinf_getf(){
    getshopinftosend();
}



void Getinftonet::topost(QString url,QByteArray date){
    network_req->setUrl(QUrl(url));
    qDebug()<<url;
    QByteArray arry;
    arry.append(QString::number(date.length()));
    network_req->setRawHeader("DateLen",arry);
    network_req->setRawHeader("DateD",date);
    network_sed->post(*network_req,"");
    arry.clear();
}





